sdApp.controller("FriendController", function($rootScope, $scope, $http, transformRequestAsFormPost){
	/* $scope.$watch('tabSelected',function(){
		console.log();
	}); */
    $scope.getFriends = function(){
        var request = $http({
            method: "get",
            url: $rootScope.urlPrefix + "/friends/",
            headers:{
                "Content-Type":"application/x-www-form-urlencoded"
            }
        });
        request.success(
            function(data){
                console.log('my Friends ' +JSON.stringify(data.records));
                if(data.records.length > 0){
                    $scope.friendsData = data.records;
                }
            }
        )
    }
    $scope.getFriends();
	$scope.isFriendSearched = false;
	
	$scope.searchFriend = function(keyword){
		console.log($scope.isFriendSearched);
		$scope.friendSearchKeyword = keyword;
		$scope.isFriendSearched = true;
         var request = $http({
            method: "get",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/search/users/"+ keyword,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            }
        });

        request.success(
                function (data) {
                    console.log(data);
                    $scope.friendSearchResult = data;
                }
        ); 
	}
    $scope.rejectFriend = function(removeuserid){
        console.log(removeuserid);
		
         var request = $http({
            method: "post",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/friends/reject/",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                removeuserid: removeuserid
            }
        });

        request.success(
                function (data) {
                    console.log(data);
                }
        );        
    }
    $scope.approveFriend = function(approveuserid){
        console.log(approveuserid);
		
         var request = $http({
            method: "post",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/friends/approve/",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                approveuserid: approveuserid
            }
        });

        request.success(
                function (data) {
                    if(data.message == 'User added as friend')
                        $scope.searchFriend($scope.friendSearchKeyword);
                }
        );        
    }
    $scope.addFriend = function(adduserid){
        console.log(adduserid);
		
         var request = $http({
            method: "post",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/friends/add/",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                adduserid: adduserid
            }
        });

        request.success(
                function (data) {
                    
                    if(data.message == 'Request Made')
                        $scope.searchFriend($scope.friendSearchKeyword);//console.log(data);
                    
                }
        );
        
    }
    $scope.unFriend = function(unfrienduserid){
        console.log(unfrienduserid);
		
         var request = $http({
            method: "post",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/friends/remove/",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                unfrienduserid: unfrienduserid
            }
        });

        request.success(
                function (data) {
                    console.log(data);
                }
        );
        
    }
});